package com.yxy.controller.service;



import com.yxy.common.BaseController;
import com.yxy.common.ServerResponse;
import com.yxy.entity.SportDynamic;
import com.yxy.entity.User;
import com.yxy.util.DateUtils;
import org.apache.commons.lang.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

import com.yxy.dto.SportDynamicModel;

import java.util.*;

import com.yxy.service.SportDynamicService;


/**
 * SportDynamicController控制器类
 * SportDynamic
 * SportDynamic
 * SportDynamic
 * SportDynamic
 *
 * @version <pre>
 * Author	Version		Date		Changes
 * 邓文俊    1.0  2018-01-11 16:43:07 Created
 *  </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/sportDynamics")
public class SportDynamicController extends BaseController {

    public Logger logger = LoggerFactory.getLogger(SportDynamicController.class);

    @Autowired
    private SportDynamicService sportDynamicService;


    //列表
    @RequestMapping(value = "list", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<List<SportDynamicModel>> list() throws Exception{
        List<SportDynamicModel> sportDynamicModels= new ArrayList<SportDynamicModel>();
        List<SportDynamic> sportDynamicList = sportDynamicService.findAll();
        if(sportDynamicList!=null && sportDynamicList.size()>0){
            for (SportDynamic sportDynamic:sportDynamicList) {
                SportDynamicModel sportDynamicModel = entityToModel(sportDynamic);
                sportDynamicModels.add(sportDynamicModel);
            }
        }
        return ServerResponse.createBySuccess("查询成功",sportDynamicModels);
    }

    //添加
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<SportDynamicModel> cerate(@RequestBody SportDynamicModel sportDynamicModel, HttpServletRequest request){
        SportDynamic sportDynamic = modelToEntity(sportDynamicModel);
        if(sportDynamic!=null){
            setTemplateDateAndUser(sportDynamic,request);
            if(sportDynamicService.create(sportDynamic)){
                return ServerResponse.createBySuccessMessage("添加成功");
            }
        }
        return ServerResponse.createByErrorMessage("操作失败,请重试");
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public ServerResponse delete(Integer id,HttpServletRequest request) throws Exception{
        SportDynamic sportDynamic = new SportDynamic();
        sportDynamic.setId(id);
        setTemplateDateAndUser(sportDynamic,request);
        if(sportDynamicService.delete(sportDynamic)){
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("操作失败，请重试！");
    }

    //查询用户
    @RequestMapping("findOne")
    @ResponseBody
    public ServerResponse findOne(Integer id)throws Exception{
        SportDynamic sportDynamic = sportDynamicService.findByPK(id);
        if (sportDynamic!=null){
            SportDynamicModel sportDynamicModel = entityToModel(sportDynamic);
            return ServerResponse.createBySuccess("查询成功",sportDynamicModel);
        }
        return ServerResponse.createByErrorMessage("操作失败，请重试！");
    }

    //修改
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<SportDynamicModel> update(@RequestBody SportDynamicModel sportDynamicModel,HttpServletRequest request){
        SportDynamic sportDynamic = modelToEntity(sportDynamicModel);

        setTemplateDateAndUser(sportDynamic,request);
        if(sportDynamicService.update(sportDynamic)){
            return ServerResponse.createBySuccessMessage("操作成功!");
        }
        return ServerResponse.createByErrorMessage("操作失败,请重试!");
    }

    //模糊查询
    @RequestMapping(value = "findLikeList", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse findLikeList(SportDynamicModel sportDynamicModel) throws Exception{
        SportDynamic sportDynamic = modelToEntity(sportDynamicModel);
        List<SportDynamic> sportDynamicList = sportDynamicService.findLikeList(sportDynamic);
        List<SportDynamicModel> sportDynamicModels =new ArrayList<SportDynamicModel>();
        if (sportDynamicList!=null && sportDynamicList.size()>0){
            for (SportDynamic s:sportDynamicList) {
                SportDynamicModel model = entityToModel(s);
                sportDynamicModels.add(model);
            }

        }
        return ServerResponse.createBySuccess("查询成功",sportDynamicModels);
    }


    /**
     * 统一设置 Date 和 修改用户
     *
     * @param entity
     */
    private void setTemplateDateAndUser(SportDynamic entity, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");

        if (entity == null) {
            logger.error("ERR:Date set error.");
            return;
        }
        if (entity.getId() == null && entity.getCrteateDatetime() == null) {
            //如果id为空， 就是新建
            entity.setCrteateDatetime(new Date());
            entity.setCreateBy(user.getUsername());
        } else {

            if (entity.getUpdateDatetime() == null) {
                entity.setUpdateDatetime(new Date());
            }
            if (entity.getUpdateBy() == null && user != null) {
                entity.setUpdateBy(user.getUsername());
            }
        }
    }


    /**
     * 复制 数据库端 Entity 到 前端 Entity
     *
     * @param entity 数据库端 Entity
     * @return model  前端 Model
     */
    public SportDynamicModel entityToModel(SportDynamic entity) throws Exception {
        if (entity == null) {
            return new SportDynamicModel();
        }
        SportDynamicModel model = new SportDynamicModel();
        if (entity.getSportDynamicPubDatatime() != null) {
            model.setSportDynamicPubDatatime(DateUtils.formatDateTime(entity.getSportDynamicPubDatatime()));
        }
        if (entity.getCrteateDatetime() != null) {
            model.setCrteateDatetime(DateUtils.formatDateTime(entity.getCrteateDatetime()));
            model.setPastMinutes(DateUtils.pastMinutes(entity.getCrteateDatetime()));
            model.setPastHour(DateUtils.pastHour(entity.getCrteateDatetime()));
        }
        if (entity.getUpdateDatetime() != null) {
            model.setUpdateDatetime(DateUtils.formatDateTime(entity.getUpdateDatetime()));
        }
        if (entity.getDelFlag() != null) {
            model.setDelFlag(String.valueOf(entity.getDelFlag()));
        }
        if (entity.getId() != null) {
            model.setId(String.valueOf(entity.getId()));
        }
        if (entity.getUserId() != null) {
            model.setUserId(String.valueOf(entity.getUserId()));
        }
        if (entity.getSportDynamicLikesNum() != null) {
            model.setSportDynamicLikesNum(String.valueOf(entity.getSportDynamicLikesNum()));
        }
        if (entity.getYxySportDynamicExtend4() != null) {
            model.setYxySportDynamicExtend4(String.valueOf(entity.getYxySportDynamicExtend4()));
        }
        if (entity.getYxySportDynamicExtend5() != null) {
            model.setYxySportDynamicExtend5(String.valueOf(entity.getYxySportDynamicExtend5()));
        }
        if (StringUtils.isNotBlank(entity.getSportDynamicTitle())) {
            model.setSportDynamicTitle(String.valueOf(entity.getSportDynamicTitle()));
        }
        if (StringUtils.isNotBlank(entity.getSportDynamicContent())) {
            model.setSportDynamicContent(String.valueOf(entity.getSportDynamicContent()));
        }
        if (StringUtils.isNotBlank(entity.getSportDynamicImg())) {
            model.setSportDynamicImg(String.valueOf(entity.getSportDynamicImg()));
        }
        if (StringUtils.isNotBlank(entity.getSportDynamicStatus())) {
            model.setSportDynamicStatus(String.valueOf(entity.getSportDynamicStatus()));
        }
        if (StringUtils.isNotBlank(entity.getUpdateBy())) {
            model.setUpdateBy(String.valueOf(entity.getUpdateBy()));
        }
        if (StringUtils.isNotBlank(entity.getCreateBy())) {
            model.setCreateBy(String.valueOf(entity.getCreateBy()));
        }
        if (StringUtils.isNotBlank(entity.getYxySportDynamicExtend1())) {
            model.setYxySportDynamicExtend1(String.valueOf(entity.getYxySportDynamicExtend1()));
        }
        if (StringUtils.isNotBlank(entity.getYxySportDynamicExtend2())) {
            model.setYxySportDynamicExtend2(String.valueOf(entity.getYxySportDynamicExtend2()));
        }
        if (StringUtils.isNotBlank(entity.getYxySportDynamicExtend3())) {
            model.setYxySportDynamicExtend3(String.valueOf(entity.getYxySportDynamicExtend3()));
        }
        if(StringUtils.isNotBlank(entity.getNickname())){
            model.setNickname(String.valueOf(entity.getNickname()));
        }
        if(StringUtils.isNotBlank(entity.getUserFace())){
            model.setUserFace(String.valueOf(entity.getUserFace()));
        }
        if(entity.getSex()!=null){
            model.setSex(entity.getSex());
        }
        return model;
    }

    /**
     * 复制前端 Model 到 数据库端 Entity
     *
     * @param model 前端 Model
     * @return entity 数据库端 Entity
     */
    public SportDynamic modelToEntity(SportDynamicModel model) {
        if (model == null) {
            return new SportDynamic();
        }
        SportDynamic entity = new SportDynamic();
        if (StringUtils.isNotBlank(model.getSportDynamicPubDatatime())) {
            entity.setSportDynamicPubDatatime(DateUtils.parseDate(model.getSportDynamicPubDatatime()));
        }
        if (StringUtils.isNotBlank(model.getCrteateDatetime())) {
            entity.setCrteateDatetime(DateUtils.parseDate(model.getCrteateDatetime()));
        }
        if (StringUtils.isNotBlank(model.getUpdateDatetime())) {
            entity.setUpdateDatetime(DateUtils.parseDate(model.getUpdateDatetime()));
        }
        if (StringUtils.isNotBlank(model.getDelFlag())) {
            entity.setDelFlag(Integer.parseInt(model.getDelFlag()));
        }
        if (StringUtils.isNotBlank(model.getId())) {
            entity.setId(Integer.parseInt(model.getId()));
        }
        if (StringUtils.isNotBlank(model.getUserId())) {
            entity.setUserId(Integer.parseInt(model.getUserId()));
        }
        if (StringUtils.isNotBlank(model.getSportDynamicLikesNum())) {
            entity.setSportDynamicLikesNum(Integer.parseInt(model.getSportDynamicLikesNum()));
        }
        if (StringUtils.isNotBlank(model.getYxySportDynamicExtend4())) {
            entity.setYxySportDynamicExtend4(Integer.parseInt(model.getYxySportDynamicExtend4()));
        }
        if (StringUtils.isNotBlank(model.getYxySportDynamicExtend5())) {
            entity.setYxySportDynamicExtend5(Integer.parseInt(model.getYxySportDynamicExtend5()));
        }
        entity.setSportDynamicTitle(model.getSportDynamicTitle());
        entity.setSportDynamicContent(model.getSportDynamicContent());
        entity.setSportDynamicImg(model.getSportDynamicImg());
        entity.setSportDynamicStatus(model.getSportDynamicStatus());
        entity.setUpdateBy(model.getUpdateBy());
        entity.setCreateBy(model.getCreateBy());
        entity.setYxySportDynamicExtend1(model.getYxySportDynamicExtend1());
        entity.setYxySportDynamicExtend2(model.getYxySportDynamicExtend2());
        entity.setYxySportDynamicExtend3(model.getYxySportDynamicExtend3());

        return entity;
    }

}